//
//  LFPlaceHolderView.m
//  TransformTest
//
//  Created by LF on 2017/6/14.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFPlaceHolderView.h"
#import "LFPlaceHolderWaveView.h"
#import "AppDelegate.h"
#define IMAGE_WITH_NAME(A) [UIImage imageNamed:A]
#define kImageW kNumFrom375(100)

@interface LFPlaceHolderView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (nonatomic, assign) Type currentType; /**< 当前状态 */
@property (nonatomic, copy) NSString *currentButtonText; /**< 当前按钮的文字 */
@property (nonatomic, strong) LFPlaceHolderWaveView *waveView; /**< 波浪view */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageConsH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageConsW;
@property (nonatomic, assign) BOOL firstLoad; /**< 判断是否第一次加载 */

@end

@implementation LFPlaceHolderView


+ (instancetype) placeHolderView {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.firstLoad = YES;
    self.type = Type_hide;
    self.firstLoad = NO;
    self.label.font = [UIFont systemFontOfSize:kNumFrom375(15)];
    self.button.titleLabel.font = [UIFont systemFontOfSize:kNumFrom375(15)];
    
    
//    [self.label setFont:[UIFont fontWithName:@"Zapfino" size:18]];
    self.failedText = @"加载失败";
    self.loadingText = @"玩命加载中..";
    self.buttonText = @"点击重新加载";
    [self.button setTitle:@"点击重新加载" forState:0];
    
    self.waveView = [[LFPlaceHolderWaveView alloc] init];
    [self addSubview:self.waveView];
    [self insertSubview:self.waveView belowSubview:self.imageView];
}

- (void)layoutSubviews {
    [self.waveView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.imageView);
    }];
}

- (IBAction)buttonClick:(id)sender {
    self.type = Type_Loading;
    if (_buttonClickBlock) {
        _buttonClickBlock(self);
    }
}

- (void)showError:(NSError *)error {
    @weakify(self)
    
    if (error.code == CUSTOM_ERROR_CODE) {
        self.failedText = error.localizedDescription;
        [self.button setTitle:self.buttonText forState:0];
    } else if (error.code == Not_Login_Code) {
        self.failedText = error.localizedDescription;
        [self.button setTitle:@"点击登录" forState:0];
        self.buttonClickBlock = ^(LFPlaceHolderView *view) {
            @strongify(self)
            [self pushToLoginVc];
        };
    } else {
        [self.button setTitle:self.buttonText forState:0];
        self.failedText = NetError;
    }
    self.type = Type_Failed_Action;
    self.currentType = self.type;
}

- (void) pushToLoginVc {
    self.type = self.currentType;
    UIViewController *loginVc = [[UIViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
    AppDelegate *app = APPDELEGATE;
    typedef void (^Animation)(void);
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        app.window.rootViewController = nav;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:app.window
                      duration:0.3f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:nil];
//    YWXLoginViewController *loginVc = [[YWXLoginViewController alloc] init];
//    [CURRENT_NAV pushViewController:loginVc animated:YES];
}



- (void)setType:(Type)type {
    _type = type;
    
    switch (type) {
        case Type_hide:
        {
            
        }
            break;
        case Type_Loading:
        {
            self.label.text = self.loadingText;
            self.imageView.image = IMAGE_WITH_NAME(@"icon_loading_loading");
            self.button.hidden = YES;
        }
            break;
            
        case Type_Failed:
        {
            self.label.text = self.failedText;
            self.button.hidden = YES;
            self.imageView.image = IMAGE_WITH_NAME(@"icon_loading_nodata");
        }
            break;
            
        case Type_Failed_Action:
        {
            self.label.text = self.failedText;
            self.button.hidden = NO;
            self.imageView.image = IMAGE_WITH_NAME(@"icon_loading_nodata");
        }
            break;
            
        case Type_Special:
        {
            
        }
            break;
            
        case Type_NoData:
        {
//            self.label.text = self.failedText;
            self.label.text = @"暂无内容信息";
            self.button.hidden = YES;
            self.imageView.image = IMAGE_WITH_NAME(@"icon_loading_nodata");
        }
            break;

            
        default:
            break;
    }
    
    if (type == Type_hide) {
        if (!self.firstLoad) {
            [UIView animateWithDuration:0.5 animations:^{
                self.alpha = 0;
            } completion:^(BOOL finished) {
                self.hidden = YES;
                self.alpha = 1;
            }];
        } else {
            self.hidden = YES;
        }
    } else {
        self.hidden = NO;
    }
//    self.hidden = type == Type_hide;
    self.waveView.hidden = type != Type_Loading;
    
    if (type == Type_Loading) {
        self.imageConsH.constant = kHalfHeight * 2;
        self.imageConsW.constant = self.imageConsH.constant / 119 * 291;
    } else {
        self.imageConsH.constant = kNumFrom375(120);
        self.imageConsW.constant = self.imageConsH.constant / 329 * 392;
    }
}

- (void)configWithLoadingText:(NSString *)loadingText failedText:(NSString *)failedText buttonText:(NSString *)buttonText block:(void (^)(LFPlaceHolderView *x))buttonBlock {
    if (loadingText.length > 0) {
        self.loadingText = loadingText;
    }
    if (failedText.length > 0) {
        self.failedText = failedText;
    }
    if (buttonBlock) {
        self.buttonClickBlock = buttonBlock;
    }
    if (buttonText) {
        self.buttonText = buttonText;
        [self.button setTitle:buttonText forState:UIControlStateNormal];
    }
}

- (void)setFailedText:(NSString *)failedText {
    _failedText = failedText;
    self.label.text = _failedText;
}

@end
